﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test
{
    public partial class WinPager : Form
    {
        List<TestUserInfo> list = new List<TestUserInfo>();

        public WinPager()
        {
            InitializeComponent();
        }

        private void WinPager_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 200; i++) //插入200条数据。
            {
                TestUserInfo user = new TestUserInfo
                {
                    UserId = "UserId" + i,
                    UserName = "UserName" + i,
                    UserEnabled = i % 2 == 0
                };
                list.Add(user);
            }

            //datagridview设置Checkbox列，绑定字符串会报DataError事件的错
            list[0].TestError = "test";

            pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
            LoadData();
        }

        void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
        void LoadData()
        {
            int count = list.Count;

            var data = list.Skip((pagerControl1.PageIndex - 1) * pagerControl1.PageSize).Take(pagerControl1.PageSize);
            gvUsers.DataSource = data.ToList();

            pagerControl1.DrawControl(count);

        }

        /// <summary>
        /// 添加自动编号
        /// </summary>
        private void gvUsers_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               gvUsers.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   gvUsers.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   gvUsers.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        /// <summary>
        /// 添加自动编号
        /// </summary>
        public static void DrawingRowIndex(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
        {
            //描绘文本的位置 
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgv.RowHeadersWidth - 13,//行头宽 
               e.RowBounds.Height);//宽 

            //描绘行头文本 
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),//内容 
                dgv.RowHeadersDefaultCellStyle.Font,//字体 
                rectangle,//位置 
                dgv.RowHeadersDefaultCellStyle.ForeColor,//颜色 
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void gvUsers_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            string error = ($"【DataError】报错！ {e.Context.ToString()} Row:{e.RowIndex} Column:{e.ColumnIndex} Exception:{e.Exception.Message}");
            e.ThrowException = false;
        }

    }

    public class TestUserInfo
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public bool UserEnabled { get; set; }

        /// <summary>
        /// 用来测试DataError事件报错
        /// </summary>
        public string TestError { get; set; }

        public byte[] Bytes { get; set; }
    }
}
